package com.swt.testdemo.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.swt.testdemo.config.ResponseBean;
import com.swt.testdemo.config.ResponseMessage;
import com.swt.testdemo.dao.SysDataLawDao;
import com.swt.testdemo.dao.SysDataLawLabelDao;
import com.swt.testdemo.entity.SysDataLawEntity;
import com.swt.testdemo.entity.SysDataLawLabelEntity;
import com.swt.testdemo.exception.AgyException;
import com.swt.testdemo.form.AddLawForm;
import com.swt.testdemo.form.AddLawLabelForm;
import com.swt.testdemo.form.QueryLawForm;
import com.swt.testdemo.service.SysDataLawLabelService;
import com.swt.testdemo.service.SysDataLawService;
import com.swt.testdemo.util.RedisUtil;
import com.swt.testdemo.util.ValidatorUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author BleethNie
 * @version 1.0
 * @date 2020/11/16 15:32
 **/
@Service("SysDataLawService")
public class SysDataLawServiceImpl extends ServiceImpl<SysDataLawDao, SysDataLawEntity> implements SysDataLawService {

    @Autowired
    private SysDataLawDao sysDataLawDao;

    @Autowired
    private SysDataLawLabelService sysDataLawLabelService;

    @Autowired
    private SysDataLawLabelDao sysDataLawLabelDao;


    @Autowired
    private RedisUtil redisUtil;

    @Override
    public ResponseBean getLawList(QueryLawForm form) {
        //参数校验
        ValidatorUtil.validateEntity(form);


        Integer page = form.getPage();
        Integer limit = form.getLimit();
        String titleFilter = form.getTitleFilter();
        Integer templateIdFilter = form.getTemplateIdFilter();
        String publishStartDateFilter = form.getPublishStartDateFilter();
        String publishEndDateFilter = form.getPublishEndDateFilter();
        String implementStartDateFilter = form.getImplementStartDateFilter();
        String implementEndDateFilter = form.getImplementEndDateFilter();
        String publishDeptFilter = form.getPublishDeptFilter();

        Page<SysDataLawEntity> entityPage = new Page<>(page, limit);
        IPage<SysDataLawEntity> resultPage = sysDataLawDao.queryDataLawList(entityPage,
                titleFilter,
                templateIdFilter,
                publishStartDateFilter,
                publishEndDateFilter,
                publishDeptFilter,
                implementStartDateFilter,
                implementEndDateFilter);



        return new ResponseBean(200,"查询成功",resultPage);
    }

    @Override
    public ResponseBean manualAddLaw(AddLawForm form) {
        return null;
    }

    @Override
    public ResponseBean addLaw(AddLawForm form) {
       return null;
    }

    @Override
    public ResponseBean getLawInfo(Integer lawId) {
        SysDataLawEntity sysDataLawEntity = sysDataLawDao.selectById(lawId);
        if (sysDataLawEntity == null){
            throw new AgyException("查询失败，该法律法规不存在");
        }

        String lawLabelId = sysDataLawEntity.getLawLabelId();
        List<Integer> labelList = Convert.toList(Integer.class, lawLabelId);
        String contentText = sysDataLawEntity.getContentText();

        List<SysDataLawLabelEntity> allLabelList = sysDataLawLabelService.getAllLabel();
        Map<Integer, String> idMap = CollUtil.fieldValueAsMap(allLabelList, "id", "labelName");

        JSONArray jsonArray = new JSONArray();
        if (labelList != null){
            for (Integer integer : labelList) {
                String name = idMap.get(integer);
                JSONObject jsonObject = new JSONObject();
                jsonObject.set("key",integer);
                jsonObject.set("name",name);
                jsonArray.put(jsonObject);

            }


        }
        JSONObject jsonObject = new JSONObject(sysDataLawEntity);
        jsonObject.set("lawLabelId",jsonArray);
        jsonObject.set("contentText", JSONUtil.parseArray(contentText));


        return new ResponseBean(200,"获取成功",jsonObject);
    }

    @Override
    public ResponseBean addLawLabel(AddLawLabelForm form) {
        DateTime current = DateUtil.date();
        Integer lawId = form.getLawId();
        String lawLabelName = form.getLawLabelName();

        //获取法律法规
        SysDataLawEntity sysDataLawEntity = this.getById(lawId);
        if (sysDataLawEntity == null){
           throw new AgyException("参数异常，没有找到对应的法律法规");
        }


        //获取包含labelname的
        SysDataLawLabelEntity sysDataLawLabelEntity = sysDataLawLabelService.getLawLabelByName(lawLabelName);
        if (sysDataLawLabelEntity == null){
            sysDataLawLabelEntity =new SysDataLawLabelEntity();
            sysDataLawLabelEntity.setLabelName(lawLabelName);
            sysDataLawLabelEntity.setId(lawId);
            sysDataLawLabelEntity.setCreatedTime(current);
            sysDataLawLabelEntity.setUpdateTime(current);
            sysDataLawLabelDao.insert(sysDataLawLabelEntity);
        }

        //中间处理
        String lawLabelId = sysDataLawEntity.getLawLabelId();
        List<String> lawLabelIdStrList = StrUtil.split(lawLabelId, ',');
        List<Integer> lawLabelIdList = new ArrayList<>();
        for (String s : lawLabelIdStrList) {
            Integer labelId = Convert.toInt(s);
            lawLabelIdList.add(labelId);
        }
        Integer sysDataLawLabelId = sysDataLawLabelEntity.getId();
        boolean contains = CollUtil.contains(lawLabelIdList, sysDataLawLabelId);
        if (contains){
            throw new AgyException("添加异常，label不可重复添加");
        }
        lawLabelId = lawLabelId + ","+sysDataLawLabelId;
        sysDataLawEntity.setLawLabelId(lawLabelId);


        //保存到数据库
        this.updateById(sysDataLawEntity);

        return new ResponseBean(200,"添加成功",sysDataLawLabelEntity);
    }

    @Override
    public ResponseMessage removeLaw(Integer lawId) {
        SysDataLawEntity sysDataLawEntity = this.getById(lawId);
        if (sysDataLawEntity == null){
            throw new AgyException("你所要删除的法律内容不存在，请检查后重试.....");
        }
        this.removeById(lawId);

        return new ResponseMessage(200,"删除成功");
    }


}
